home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / openfi.exe / OPENFILE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-04-17  |  3.3 KB  |  112 lines

  1. Program OpenFileExample;
  2.  
  3. {$R openfile}
  4.  
  5. Uses WIN31, WINPROCS, WINTYPES, COMMDLG;
  6.  
  7. Var
  8.   ofn : TOpenFileName;
  9.   FileName : Array [0..255] Of Char;
  10.   FileTitle : Array[0..12] Of Char;
  11.   WM_FILEOK : Word;
  12.   Icon : HIcon;
  13.  
  14. Const
  15.   Filters : PChar = 'C source files (*.c)'+#0+'*.c'+#0+
  16.                     'C++ source files (*.cpp)'+#0+'*.cpp'+#0+
  17.                     'Pascal source files (*.pas)'+#0+'*.pas'+#0+
  18.                     #0;
  19.  
  20. { This is the hook function. In here we can trap messages sent to   }
  21. { the File Open dialog and process them before the normal code gets }
  22. { a chance. By returning anything other than 0, we can say to the   }
  23. { original procedure "I've processed this message, so you should    }
  24. { ignore it".                                                       }
  25.  
  26. Function FileOpenHook(Wnd : HWnd; Msg, wParam : Word; lParam : Longint) : Word; Export;
  27.  
  28.   { Dialogs don't normally have icons associated with them, so we    }
  29.   { process this message ourselves in order to paint the icon. Note  }
  30.   { that we only do this when the window is an icon - otherwise, we  }
  31.   { let the normal processing continue.                              }
  32.  
  33.   Function WMPaint : Word;
  34.   Var
  35.     ps : TPaintStruct;
  36.     dc : HDC;
  37.   Begin
  38.     WMPaint := 0;
  39.     If IsIconic(Wnd) Then
  40.     Begin
  41.       dc := BeginPaint(Wnd, ps);
  42.       SendMessage(Wnd, WM_ICONERASEBKGND, dc, 0);
  43.       DrawIcon(dc, 0, 0, Icon);
  44.       EndPaint(Wnd, ps);
  45.       WMPaint := 1;
  46.     End;
  47.   End;
  48.  
  49.   { This message lets us know when the Ok button has been clicked. By  }
  50.   { intercepting it and always returning 1 we can prevent the dialog   }
  51.   { from closing. At this point, FileName now has the name of the file }
  52.   { selected or typed by the user.                                     }
  53.  
  54.   Function WMFileOk : Word;
  55.   Begin
  56.     MessageBox(Wnd, FileName, 'Ok button clicked!', MB_OK+MB_ICONINFORMATION);
  57.     WMFileOk := 1;
  58.   End;
  59.  
  60. Begin
  61.   Case Msg Of
  62.     WM_PAINT         : FileOpenHook := WMPaint;
  63.     WM_QUERYDRAGICON : FileOpenHook := Icon;
  64.   Else
  65.     If Msg = WM_FILEOK Then
  66.       FileOpenHook := WMFileOk
  67.     Else
  68.       FileOpenHook := 0;
  69.   End;
  70. End;
  71.  
  72. Begin
  73.  
  74.   { First we must initialize the TOpenFileName structure used by GetOpenFileName. }
  75.  
  76.   With ofn Do
  77.   Begin
  78.     lStructSize := SizeOf(TOpenFileName);
  79.     hwndOwner := 0;
  80.     hInstance := System.HInstance;
  81.     lpstrFilter := Filters;
  82.     lpstrCustomFilter := Nil;
  83.     nMaxCustFilter := 0;
  84.     nFilterIndex := 0;
  85.     lpstrFile := @FileName;
  86.     nMaxFile := sizeof(FileName);
  87.     lpstrFileTitle := @FileTitle;
  88.     nMaxFileTitle := sizeof(FileTitle);
  89.     lpstrInitialDir := Nil;
  90.     lpstrTitle := 'File Open Example';
  91.     Flags := OFN_HIDEREADONLY + OFN_PATHMUSTEXIST + OFN_ENABLETEMPLATE + OFN_ENABLEHOOK;
  92.     lpstrDefExt := 'pas';
  93.     lCustData := 0;
  94.     lpfnHook := FileOpenHook;
  95.     lpTemplateName := 'MAIN';
  96.   End;
  97.  
  98.   { Now we get the value of the message that COMMDLG sends when the OK button }
  99.   { is clicked.                                                               }
  100.  
  101.   WM_FILEOK := RegisterWindowMessage(FILEOKSTRING);
  102.  
  103.   { Get a handle to the application's icon. }
  104.  
  105.   Icon := LoadIcon(HInstance, 'MAIN');
  106.  
  107.   { And we're off to the races! }
  108.  
  109.   GetOpenFileName(ofn);
  110.  
  111.   DestroyIcon(Icon);
  112. End.